////package com.baibei.module.stock.basic;
////
////import android.content.Context;
////import android.util.SparseArray;
////
////import com.baibei.basic.BasicPresenterImpl;
////import com.baibei.ebec.quotation.AppQuotationFilter;
////import com.baibei.ebec.quotation.QuotationManager;
////import com.baibei.ebec.quotation.event.IQuotationEvent;
////import com.baibei.ebec.sdk.ApiFactory;
////import com.baibei.ebec.sdk.IQuotationApi;
////import com.baibei.model.MarketStatusInfo;
////import com.baibei.model.MinuteEntry;
////import com.baibei.model.QuotationInfo;
////import com.baibei.model.TradeTimeInfo;
////import com.baibei.quotation.QuotationFilter;
////import com.baibei.sdk.ApiDefaultObserver;
////import com.blankj.utilcode.utils.LogUtils;
////
////import org.greenrobot.eventbus.Subscribe;
////
////import java.util.List;
////import java.util.concurrent.TimeUnit;
////
////import io.reactivex.Observable;
////import io.reactivex.android.schedulers.AndroidSchedulers;
////import io.reactivex.disposables.Disposable;
////import io.reactivex.functions.Consumer;
////import io.reactivex.schedulers.Schedulers;
////
/////**
//// * 分时图
//// * Created by ChenRui on 2017/5/18 0018 15:11.
//// */
////public class StockMinutePresenterImpl extends BasicPresenterImpl<StockMinuteContract.View> implements StockMinuteContract.Presenter, IQuotationEvent {
////
////    private IQuotationApi mQuotationApi;
////    private Disposable mIntervalSubscriber;
////    /**
////     * 记录当前市场状态
////     */
////    private int mCurrentMarketStatus;
////
////    public StockMinutePresenterImpl(Context context, StockMinuteContract.View view) {
////        super(context, view);
////        mQuotationApi = ApiFactory.getInstance().getQuotationApi();
////    }
////
////    @Override
////    public void start() {
////        super.start();
////        registerMarketStatus();
////    }
////
////    @Override
////    public void destroy() {
////        unRegisterMarketStatus();
////        stopInterval();
////        super.destroy();
////    }
////
////    /**
////     * 停止刷新数据
////     */
////    @Override
////    public void stopInterval() {
////        if (mIntervalSubscriber != null && !mIntervalSubscriber.isDisposed()) {
////            mIntervalSubscriber.dispose();
////            mIntervalSubscriber = null;
////        }
////    }
////
////    @Override
////    public void registerMarketStatus() {
////        //通过接口获取最新的开休市状态，再通过websocket进行开休市监听
////        createObservable(mQuotationApi.getTradeTimeInfo()).subscribe(new ApiDefaultObserver<TradeTimeInfo>() {
////            @Override
////            protected void onError(String s) {
////                if (mView != null) {
////                    mView.onLoadChartDataFailed(IQuotationApi.TYPE_MINUTE, s);
////                }
////            }
////
////            @Override
////            protected void accept(TradeTimeInfo info) {
////                //保存接口返回状态，用于比较websocket推送状态
////                mCurrentMarketStatus = info.getFlag();
////                if (info.getFlag() == 1) {
////                    loadMinuteData();
////                    startInterval();
////                } else {
////                    if (mView != null) {
////                        mView.onTradeOutTime();
////                    }
////                }
////                //注册
////                QuotationFilter filter = new QuotationFilter(QuotationFilter.ACTION_QUOTATION_PRODUCT);
////                filter.addAction(QuotationFilter.ACTION_QUOTATION_TRADE_STATUS);
////                filter.addAction(QuotationFilter.ACTION_QUOTATION_TRADE_TAP);
////                filter.addAction(AppQuotationFilter.ACTION_MARKET_STATUS);
////                QuotationManager.getInstance().register(StockMinutePresenterImpl.this, filter);
////            }
////        });
////    }
////
////    @Override
////    public void unRegisterMarketStatus() {
////        QuotationManager.getInstance().unregister(this);
////    }
////
////
////    @Subscribe
////    public void onEvent(MarketStatusInfo statusInfo) {
////        LogUtils.e("开休市推送", statusInfo + ";是否需要更新状态:" + (mCurrentMarketStatus != statusInfo.getStatus()) + ";by" + mView);
////        //如果开休市状态一致，取消操作
////        if (mCurrentMarketStatus == statusInfo.getStatus()) {
////            return;
////        }
////        LogUtils.e("开休市推送", "状态不一致，重新改变状态");
////        //如果不一致，操作
////        mCurrentMarketStatus = statusInfo.getStatus();
////        stopInterval();
////        //开市了
////        if (mCurrentMarketStatus == 1) {
////            loadMinuteData();
////            startInterval();
////            return;
////        }
////        //休市了
////        if (mView != null) {
////            mView.onTradeOutTime();
////        }
////    }
////
////
////    /**
////     * 轮询刷新数据
////     */
////    @Override
////    public void startInterval() {
////        if (mIntervalSubscriber != null && !mIntervalSubscriber.isDisposed()) {
////            return;
////        }
////        mIntervalSubscriber = Observable.interval(30, TimeUnit.SECONDS)
////                .subscribeOn(Schedulers.newThread())
////                .observeOn(AndroidSchedulers.mainThread())
////                .subscribe(new Consumer<Long>() {
////                    @Override
////                    public void accept(Long aLong) throws Exception {
////                        loadMinuteData();
////                    }
////                });
////    }
////
////    private void loadMinuteData() {
////        createObservable(mQuotationApi.getMinuteData(mView.getProductId(), IQuotationApi.TYPE_MINUTE))
////                .subscribe(new ApiDefaultObserver<List<MinuteEntry>>() {
////                    @Override
////                    protected void onError(String s) {
////                        if (mView == null) return;
////                        mView.onLoadChartDataFailed(IQuotationApi.TYPE_MINUTE, s);
////                    }
////
////                    @Override
////                    protected void accept(List<MinuteEntry> entries) {
////                        LogUtils.e("K线数据", ";by" + mView);
////                        if (mView == null) return;
////                        mView.onLoadMinuteChartData(entries, 0L);
////                    }
////                });
////    }
////
////
////    /**
////     * 行情事件
////     */
////    @Override
////    @Subscribe
////    public void onEvent(SparseArray<QuotationInfo> quotation) {
////
////    }
////}
//
//package com.baibei.module.stock.basic;
//
//import android.content.Context;
//import android.util.Log;
//import android.util.SparseArray;
//
//import com.baibei.basic.BasicPresenterImpl;
//import com.baibei.ebec.quotation.AppQuotationFilter;
//import com.baibei.ebec.quotation.QuotationManager;
//import com.baibei.ebec.quotation.event.IQuotationEvent;
//import com.baibei.ebec.sdk.ApiFactory;
//import com.baibei.ebec.sdk.IQuotationApi;
//import com.baibei.model.MarketStatusInfo;
//import com.baibei.model.MinuteEntry;
//import com.baibei.model.QuotationInfo;
//import com.baibei.model.TradeTimeInfo;
//import com.baibei.quotation.QuotationFilter;
//import com.baibei.sdk.ApiDefaultObserver;
//import com.blankj.utilcode.utils.LogUtils;
//
//import org.greenrobot.eventbus.Subscribe;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
//import io.reactivex.Observable;
//import io.reactivex.android.schedulers.AndroidSchedulers;
//import io.reactivex.disposables.Disposable;
//import io.reactivex.functions.Consumer;
//import io.reactivex.schedulers.Schedulers;
//
//import static android.content.ContentValues.TAG;
//
///**
// * 分时图
// * Created by ChenRui on 2017/5/18 0018 15:11.
// */
//public class StockMinutePresenterImpl extends BasicPresenterImpl<StockMinuteContract.View> implements StockMinuteContract.Presenter, IQuotationEvent {
//
//    private IQuotationApi mQuotationApi;
//    private Disposable mIntervalSubscriber;
//    /**
//     * 记录当前市场状态
//     */
//    private int mCurrentMarketStatus;
////    private ArrayList<String> timePoints = new ArrayList<>();
//
//    public StockMinutePresenterImpl(Context context, StockMinuteContract.View view) {
//        super(context, view);
//        mQuotationApi = ApiFactory.getInstance().getQuotationApi();
//    }
//
//    @Override
//    public void start() {
//        super.start();
//        registerMarketStatus();
//    }
//
//    @Override
//    public void destroy() {
//        unRegisterMarketStatus();
//        stopInterval();
//        super.destroy();
//    }
//
//    /**
//     * 停止刷新数据
//     */
//    @Override
//    public void stopInterval() {
//        if (mIntervalSubscriber != null && !mIntervalSubscriber.isDisposed()) {
//            mIntervalSubscriber.dispose();
//            mIntervalSubscriber = null;
//        }
//    }
//
//    @Override
//    public void registerMarketStatus() {
//        //通过接口获取最新的开休市状态，再通过websocket进行开休市监听
//        createObservable(mQuotationApi.getTradeTimeInfo()).subscribe(new ApiDefaultObserver<TradeTimeInfo>() {
//            @Override
//            protected void onError(String s) {
//                if (mView != null) {
//                    mView.onLoadChartDataFailed(IQuotationApi.TYPE_MINUTE, s);
//                }
//            }
//
//            @Override
//            protected void accept(TradeTimeInfo info) {
//                //保存接口返回状态，用于比较websocket推送状态
//                mCurrentMarketStatus = info.getFlag();
//                if (info.getFlag() == 1) {
//                    if (mView == null) return;
//                    loadMinuteData();
//                    startInterval();
//                } else {
//                    if (mView != null) {
//                        mView.onTradeOutTime();
//                    }
//                }
//                //注册
//                QuotationFilter filter = new QuotationFilter(QuotationFilter.ACTION_QUOTATION_PRODUCT);
//                filter.addAction(QuotationFilter.ACTION_QUOTATION_TRADE_STATUS);
//                filter.addAction(QuotationFilter.ACTION_QUOTATION_TRADE_TAP);
//                filter.addAction(AppQuotationFilter.ACTION_MARKET_STATUS);
//                QuotationManager.getInstance().register(StockMinutePresenterImpl.this, filter);
//            }
//        });
//    }
//
//    @Override
//    public void unRegisterMarketStatus() {
//        QuotationManager.getInstance().unregister(this);
//    }
//
//
//    @Subscribe
//    public void onEvent(MarketStatusInfo statusInfo) {
//        LogUtils.e("开休市推送", statusInfo + ";是否需要更新状态:" + (mCurrentMarketStatus != statusInfo.getStatus()) + ";by" + mView);
//        //如果开休市状态一致，取消操作
//        if (mCurrentMarketStatus == statusInfo.getStatus()) {
//            return;
//        }
//        LogUtils.e("开休市推送", "状态不一致，重新改变状态");
//        //如果不一致，操作
//        mCurrentMarketStatus = statusInfo.getStatus();
//        stopInterval();
//        //开市了
//        if (mCurrentMarketStatus == 1) {
//            if (mView == null) return;
//            loadMinuteData();
//            startInterval();
//            return;
//        }
//        //休市了
//        if (mView != null) {
//            mView.onTradeOutTime();
//        }
//    }
//
//
//    /**
//     * 轮询刷新数据
//     */
//    @Override
//    public void startInterval() {
//        if (mIntervalSubscriber != null && !mIntervalSubscriber.isDisposed()) {
//            return;
//        }
//        mIntervalSubscriber = Observable.interval(30, TimeUnit.SECONDS)
//                .subscribeOn(Schedulers.newThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<Long>() {
//                    @Override
//                    public void accept(Long aLong) throws Exception {
//                        if (mView == null) return;
//                        loadMinuteData();
//                    }
//                });
//    }
//
//    private void loadMinuteData() {
//
//        createObservable(mQuotationApi.getMinuteData(mView.getProductInfo(), IQuotationApi.TYPE_MINUTE))
//                .subscribe(new ApiDefaultObserver<List<MinuteEntry>>() {
//                    @Override
//                    protected void onError(String s) {
//                        if (mView == null) return;
//                        mView.onLoadChartDataFailed(IQuotationApi.TYPE_MINUTE, s);
//                    }
//
//                    @Override
//                    protected void accept(List<MinuteEntry> entries) {
////                        timePoints = new ArrayList<String>(){{add("11:20");add("11:22");add("14:40");add("14:42");add("18:00");add("18:02");add("21:20");add("21:22");add("00:40");add("00:42");}};
////                        for (int i = 1 ; i < entries.size()-1 ; i++){
////                            if (!"8:00".equals(entries.get(i).getTime())&&!"9:00".equals(entries.get(i).getTime())){
////                                entries.get(i).setTime("");
////                            }
////                        }
//
//                        LogUtils.e("K线数据", ";by" + mView);
//                        if (mView == null) return;
//                        mView.onLoadMinuteChartData(entries, 0L);
//                    }
//                });
//    }
//
//
//    /**
//     * 行情事件
//     */
//    @Override
//    @Subscribe
//    public void onEvent(SparseArray<QuotationInfo> quotation) {
//
//    }
//}

package com.baibei.module.stock.basic;

import android.content.Context;
import android.util.SparseArray;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.ebec.quotation.AppQuotationFilter;
import com.baibei.ebec.quotation.QuotationManager;
import com.baibei.ebec.quotation.event.IQuotationEvent;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IQuotationApi;
import com.baibei.model.MarketStatusInfo;
import com.baibei.model.MinuteEntry;
import com.baibei.model.QuotationInfo;
import com.baibei.model.TradeTimeInfo;
import com.baibei.quotation.QuotationFilter;
import com.baibei.sdk.ApiDefaultObserver;
import com.blankj.utilcode.utils.LogUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 分时图
 * Created by ChenRui on 2017/5/18 0018 15:11.
 */
public class StockMinutePresenterImpl extends BasicPresenterImpl<StockMinuteContract.View> implements StockMinuteContract.Presenter, IQuotationEvent {

    private IQuotationApi mQuotationApi;
    private Disposable mIntervalSubscriber;
    /**
     * 记录当前市场状态
     */
    private int mCurrentMarketStatus;

    public StockMinutePresenterImpl(Context context, StockMinuteContract.View view) {
        super(context, view);
        mQuotationApi = ApiFactory.getInstance().getQuotationApi();
    }

    @Override
    public void start() {
        super.start();
        registerMarketStatus();
    }

    @Override
    public void destroy() {
        unRegisterMarketStatus();
        stopInterval();
        super.destroy();
    }

    /**
     * 停止刷新数据
     */
    @Override
    public void stopInterval() {
        if (mIntervalSubscriber != null && !mIntervalSubscriber.isDisposed()) {
            mIntervalSubscriber.dispose();
            mIntervalSubscriber = null;
        }
    }

    @Override
    public void registerMarketStatus() {
        //通过接口获取最新的开休市状态，再通过websocket进行开休市监听
        createObservable(mQuotationApi.getTradeTimeInfo()).subscribe(new ApiDefaultObserver<TradeTimeInfo>() {
            @Override
            protected void onError(String s) {
                if (mView != null) {
                    mView.onLoadChartDataFailed(IQuotationApi.TYPE_MINUTE, s);
                }
            }

            @Override
            protected void accept(TradeTimeInfo info) {
                //保存接口返回状态，用于比较websocket推送状态
                mCurrentMarketStatus = info.getFlag();
                if (info.getFlag() == 1) {
                    if (mView == null) return;
                    loadMinuteData();
                    startInterval();
                } else {
                    if (mView != null) {
                        mView.onTradeOutTime();
                    }
                }
                //注册
                QuotationFilter filter = new QuotationFilter(QuotationFilter.ACTION_QUOTATION_PRODUCT);
                filter.addAction(QuotationFilter.ACTION_QUOTATION_TRADE_STATUS);
                filter.addAction(QuotationFilter.ACTION_QUOTATION_TRADE_TAP);
                filter.addAction(AppQuotationFilter.ACTION_MARKET_STATUS);
                QuotationManager.getInstance().register(StockMinutePresenterImpl.this, filter);
            }
        });
    }

    @Override
    public void unRegisterMarketStatus() {
        QuotationManager.getInstance().unregister(this);
    }


    @Subscribe
    public void onEvent(MarketStatusInfo statusInfo) {
        LogUtils.e("开休市推送", statusInfo + ";是否需要更新状态:" + (mCurrentMarketStatus != statusInfo.getStatus()) + ";by" + mView);
        //如果开休市状态一致，取消操作
        if (mCurrentMarketStatus == statusInfo.getStatus()) {
            return;
        }
        LogUtils.e("开休市推送", "状态不一致，重新改变状态");
        //如果不一致，操作
        mCurrentMarketStatus = statusInfo.getStatus();
        stopInterval();
        //开市了
        if (mCurrentMarketStatus == 1) {
            if (mView == null) return;
            loadMinuteData();
            startInterval();
            return;
        }
        //休市了
        if (mView != null) {
            mView.onTradeOutTime();
        }
    }


    /**
     * 轮询刷新数据
     */
    @Override
    public void startInterval() {
        if (mIntervalSubscriber != null && !mIntervalSubscriber.isDisposed()) {
            return;
        }
        mIntervalSubscriber = Observable.interval(30, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (mView == null) return;
                        loadMinuteData();
                    }
                });
    }

    private void loadMinuteData() {
        createObservable(mQuotationApi.getMinuteData(mView.getProductInfo(), IQuotationApi.TYPE_MINUTE))
                .subscribe(new ApiDefaultObserver<List<MinuteEntry>>() {
                    @Override
                    protected void onError(String s) {
                        if (mView == null) return;
                        mView.onLoadChartDataFailed(IQuotationApi.TYPE_MINUTE, s);
                    }

                    @Override
                    protected void accept(List<MinuteEntry> entries) {
                        LogUtils.e("K线数据", ";by" + mView);
                        if (mView == null) return;
                        mView.onLoadMinuteChartData(entries, 0L);
                    }
                });
    }


    /**
     * 行情事件
     */
    @Override
    @Subscribe
    public void onEvent(SparseArray<QuotationInfo> quotation) {

    }
}

